#include <dspSimulator/instruction/xor.h>


bool XOR::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    if (isCompactInstruction) {
        return true;
    } else {
        return checkStandardCondition(dspMachine);
    }
}

void XOR::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                            std::shared_ptr<llvm::IRBuilder<>> irBuilder) {
    llvm::FunctionType *funcType = llvm::FunctionType::get(irBuilder->getVoidTy(), false);

    // ---------------------------------XOR (.unit) op1, xop2, dst ---------------------------------------------
    {
        llvm::Function *xorL = llvm::Function::Create(funcType, llvm::GlobalValue::ExternalLinkage, "XORKind1",
                                                      module);
        llvm::BasicBlock *entryBB = llvm::BasicBlock::Create(*llvmContext, "entry", xorL);
        irBuilder->SetInsertPoint(entryBB);

        auto dstRegPtr = getDstLoRegPtr(module, irBuilder);

        auto src2RegPtr = getSrc2LoRegPtr(module, irBuilder);
        auto src2Reg = irBuilder->CreateLoad(src2RegPtr);

        auto src1RegPtr = getSrc1LoRegPtr(module, irBuilder);
        auto src1Reg = irBuilder->CreateLoad(src1RegPtr);

        auto res = irBuilder->CreateXor(src1Reg, src2Reg);

        irBuilder->CreateStore(res, dstRegPtr);
        irBuilder->CreateRetVoid();
    }

    // ---------------------------------XOR (.unit) scst5, xop2, dst  ---------------------------------------------
    {
        llvm::Function *xorL = llvm::Function::Create(funcType, llvm::GlobalValue::ExternalLinkage, "XORKind3",
                                                      module);
        llvm::BasicBlock *entryBB = llvm::BasicBlock::Create(*llvmContext, "entry", xorL);
        irBuilder->SetInsertPoint(entryBB);

        auto dstRegPtr = getDstLoRegPtr(module, irBuilder);

        auto src2RegPtr = getSrc2LoRegPtr(module, irBuilder);
        auto src2Reg = irBuilder->CreateLoad(src2RegPtr);

        auto cst5 = getCstN(module, irBuilder, 5);
        auto scst5 = irBuilder->CreateSExt(cst5, irBuilder->getInt32Ty());

        auto res = irBuilder->CreateXor(scst5, src2Reg);

        irBuilder->CreateStore(res, dstRegPtr);
        irBuilder->CreateRetVoid();
    }
}

std::shared_ptr<Instruction> XOR::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<XOR> _xor;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<10>(data, 2, 0b1101111110) && existStandardCondition(data)) {
            _xor = std::make_shared<XOR>(dspInstInfo->getAddress(), baseCycle);
            _xor->creg_z = extractBits<4>(data, 28);
            _xor->dst = extractBits<5>(data, 23).to_ulong();
            _xor->src2 = extractBits<5>(data, 18).to_ulong();
            _xor->src1 = extractBits<5>(data, 13).to_ulong();
            _xor->x = data[12];
            _xor->s = data[1];
            _xor->p = data[0];
            if (_xor->s == 0) {
                _xor->funcUnit = FuncUnit::L1;
            } else {
                _xor->funcUnit = FuncUnit::L2;
            }
            _xor->instFormat = InstFormat::Kind1;
        } else if (checkBits<10>(data, 2, 0b0010111000) && existStandardCondition(data)) {
            _xor = std::make_shared<XOR>(dspInstInfo->getAddress(), baseCycle);
            _xor->creg_z = extractBits<4>(data, 28);
            _xor->dst = extractBits<5>(data, 23).to_ulong();
            _xor->src2 = extractBits<5>(data, 18).to_ulong();
            _xor->src1 = extractBits<5>(data, 13).to_ulong();
            _xor->x = data[12];
            _xor->s = data[1];
            _xor->p = data[0];
            if (_xor->s == 0) {
                _xor->funcUnit = FuncUnit::S1;
            } else {
                _xor->funcUnit = FuncUnit::S2;
            }
            _xor->instFormat = InstFormat::Kind1;
        } else if (checkBits<10>(data, 2, 0b1011101100) && existStandardCondition(data)) {
            _xor = std::make_shared<XOR>(dspInstInfo->getAddress(), baseCycle);
            _xor->creg_z = extractBits<4>(data, 28);
            _xor->dst = extractBits<5>(data, 23).to_ulong();
            _xor->src2 = extractBits<5>(data, 18).to_ulong();
            _xor->src1 = extractBits<5>(data, 13).to_ulong();
            _xor->x = data[12];
            _xor->s = data[1];
            _xor->p = data[0];
            if (_xor->s == 0) {
                _xor->funcUnit = FuncUnit::D1;
            } else {
                _xor->funcUnit = FuncUnit::D2;
            }
            _xor->instFormat = InstFormat::Kind1;
        } else if (checkBits<10>(data, 2, 0b0010101000) && existStandardCondition(data)) {
            _xor = std::make_shared<XOR>(dspInstInfo->getAddress(), baseCycle);
            _xor->creg_z = extractBits<4>(data, 28);
            _xor->dst = extractBits<5>(data, 23).to_ulong();
            _xor->src2 = extractBits<5>(data, 18).to_ulong();
            _xor->cst = extractBits<5>(data, 13).to_ulong();
            _xor->x = data[12];
            _xor->s = data[1];
            _xor->p = data[0];
            if (_xor->s == 0) {
                _xor->funcUnit = FuncUnit::S1;
            } else {
                _xor->funcUnit = FuncUnit::S2;
            }
            _xor->instFormat = InstFormat::Kind3;
        }
    } else {
        auto data = vectorToBitSet<16>(instInfo->getBits());
        if (checkBits<6>(data, 10, 0b111110) && checkBits<2>(data, 5, 0b11) && checkBits<2>(data, 1, 0b11)) {
            _xor = std::make_shared<XOR>(dspInstInfo->getAddress(), baseCycle);
            _xor->isCompactInstruction = true;
            _xor->compactInstKind = "LSDx1";
            _xor->src2 = _xor->dst = extractBits<3>(data, 7).to_ulong();
            _xor->cst = 1;
            auto unit = extractBits<2>(data, 3).to_ulong();
            _xor->s = data[0];
            if (unit == 0b00) {
                _xor->funcUnit = FuncUnit::L1;
                if (_xor->s == 1)
                    _xor->funcUnit = FuncUnit::L2;
            } else if (unit == 0b01) {
                _xor->funcUnit = FuncUnit::S1;
                if (_xor->s == 1)
                    _xor->funcUnit = FuncUnit::S2;
            } else if (unit == 0b10) {
                _xor->funcUnit = FuncUnit::D1;
                if (_xor->s == 1)
                    _xor->funcUnit = FuncUnit::D2;
            }
            _xor->DSZ = dspInstInfo->getDSZ();
            _xor->RS = dspInstInfo->getRS();
            _xor->SAT = dspInstInfo->getSAT();
            _xor->PROT = dspInstInfo->getPROT();
            _xor->BR = dspInstInfo->getBR();
            _xor->p = dspInstInfo->getP();
            _xor->fixUpRegOnCompactInstruction();
            _xor->instFormat = InstFormat::Kind3;
        }
    }
    return _xor;
}

void XOR::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {

}

std::shared_ptr<DspInstruction> XOR::clone() {
    return std::make_shared<XOR>(*this);
}

std::string XOR::toString() const {
    std::string res;
    if (!isCompactInstruction) {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)] + " ";
    switch (instFormat) {
        case InstFormat::Kind1:
            res += getSrc1Reg32Name() + "," + getSrc2Reg32Name() + "," + getDstReg32Name();
            break;
        case InstFormat::Kind3:
            res += dec2hex(extractBitsAndToInt<5>(cst)) + "," + getSrc2Reg32Name() + "," +
                   getDstReg32Name();
            break;
        default:
            break;
    }
    return res;
}
